//
//  Seach_SearchResult_VC_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/28.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Seach_SearchResult_VC_WX.h"
#import "Seach_SearchResultTopBar_View_WX.h"
#import "Seach_SearchResult_Tbv_WX.h"
#import "Seach_Filtrate_Model_WX.h"
#import "MainAntistopVC_V4.h"
#import "Seach_FiltrateDialogs_View_WX.h"

@interface Seach_SearchResult_VC_WX ()

@property (nonatomic,strong)Seach_SearchResultTopBar_View_WX * search;

@property (nonatomic,strong)Seach_SearchResult_Tbv_WX * tbv;

@property (nonatomic,strong)Seach_FiltrateDialogs_View_WX * FiltrateDialogs;

@end

@implementation Seach_SearchResult_VC_WX

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.baseNavigationView setHiddenNav:YES VC:self];
//    [self loadConditionDate];

    kUSER_Filtrate_MANAGER.SearchResultTopBarModel.sotr = 5;
    [self setUI];

//    [self.tbv reloadData];
//
    [self setRACObserve];
}

-(void)setUI{
    
    [self.view addSubview:self.tbv];

    [self.view addSubview:self.search];
    
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]) {
        self.search.backgroundColor = Main_Token_Model_MANAGER.backgroundColor;
    }else{
        self.search.backgroundColor = [UIColor randomColor];
    }
    
    [self.FiltrateDialogs hide];
    
    [self.search mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view).offset(0);
        make.height.offset(127);
    }];
    

    [self.tbv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.left.equalTo(self.view).offset(0);
        make.height.offset(kScreenH-127);
    }];

    
}

-(void)setRACObserve{
    [RACObserve(kUSER_Filtrate_MANAGER,SearchResultTopBarModel.ResultCont)subscribeNext:^(id  _Nullable x) {
        int officialPriceeInt = [kUSER_Filtrate_MANAGER.SearchResultTopBarModel.ResultCont intValue];
    NSNumberFormatter *moneyFormatter = [[NSNumberFormatter alloc] init];
        moneyFormatter.positiveFormat = @"###,##0";
        NSString *formatString = [moneyFormatter stringFromNumber:@(officialPriceeInt)];
        self.search.leb.text = [NSString stringWithFormat:@"%@个结果",formatString];
    }];
    
    [RACObserve(kUSER_Filtrate_MANAGER,SearchResultTopBarModel.keyWord)subscribeNext:^(id  _Nullable x) {
        if (kUSER_Filtrate_MANAGER.SearchResultTopBarModel.keyWord.length>0&& ![kUSER_Filtrate_MANAGER.SearchResultTopBarModel.keyWord isEqualToString:@"筛选"]) {
            [self.search.btn2 setTitle:[NSString stringWithFormat:@"  %@",kUSER_Filtrate_MANAGER.SearchResultTopBarModel.keyWord] forState:UIControlStateNormal];
            self.FiltrateDialogs.mainView.NavView.labTitle.text = kUSER_Filtrate_MANAGER.SearchResultTopBarModel.keyWord;
            self.FiltrateDialogs.mainView.NavView.emptyBtn.hidden = NO;
        }else{
            [self.search.btn2 setTitle:@"  关键字" forState:UIControlStateNormal];
            self.FiltrateDialogs.mainView.NavView.labTitle.text = @"筛选";
            self.FiltrateDialogs.mainView.NavView.emptyBtn.hidden = YES;
            
        }
        [self.tbv.mj_header beginRefreshing];
    }];
    
    
    
    

}





-(Seach_SearchResultTopBar_View_WX *)search{
    if (!_search) {
        _search = [[Seach_SearchResultTopBar_View_WX alloc]initWithFrame:CGRectMake(0, 0, 2, 2)];
        @weakify(self);
   
        /**
         
         * 按钮点击案例
        [[_search.btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            //展开选择器
            MainDefaulSearchVC_V4 *vc = [[MainDefaulSearchVC_V4 alloc]init];
            vc.indexType = 1;
            vc.tbv.arrM = self.arrM;
            vc.tbv.model = self.model;
            
            [vc setBlock:^(NSMutableArray * arrM) {
                
                self.arrM = arrM;
                [self loadData];
            }];
            [self.navigationController pushViewController:vc animated:YES];
            
        }];
        
      
   
         */
        
        [[_search.btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            
            [self.FiltrateDialogs show];
        }];
       
        [[_search.btn2 rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            
            MainAntistopVC_V4 *vc = [[MainAntistopVC_V4 alloc]init];
            vc.indexType = 1;
            [vc setBlock:^(NSString * _Nonnull str) {
                 kUSER_Filtrate_MANAGER.SearchResultTopBarModel.keyWord = str;
            }];
            [self.navigationController pushViewController:vc animated:YES];
        }];
        
        [[_search.btn3 rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            self.search.btn3.selected =  !self.search.btn3.selected;
            //排序按钮
            if ( self.search.btn3.tag==300) {
                self.search.btn3.tag = 3001;
                kUSER_Filtrate_MANAGER.SearchResultTopBarModel.sotr = 4;

                //                [self.search.btn3 setImage:[UIImage imageNamed:@"nav_btn_ascend"] forState:UIControlStateNormal];
            }else{
                self.search.btn3.tag = 300;
                kUSER_Filtrate_MANAGER.SearchResultTopBarModel.sotr =5;


                //                [self.search.btn3 setImage:[UIImage imageNamed:@"nav_btn_descend"] forState:UIControlStateNormal];
            }
            [self.search chageBtn];
            [self.tbv.mj_header beginRefreshing];
        }];
        
    }
    return _search;
}

-(Seach_SearchResult_Tbv_WX *)tbv{
    if (!_tbv) {
        _tbv =  [[Seach_SearchResult_Tbv_WX alloc]initializeFrame:CGRectMake(0, 0, 1, 1) isMj:1 isEstimatedRowHeight:YES addRegisterNib:@[@"InquiryMain_HotGoodsList_Cell_WX"]];
    }
    return _tbv;
}


-(Seach_FiltrateDialogs_View_WX *)FiltrateDialogs{
    if (!_FiltrateDialogs) {
        _FiltrateDialogs = [[Seach_FiltrateDialogs_View_WX alloc]initWithFrame:CGRectMake(0,0, kScreenW, kScreenH)];
        @weakify(self);
        [[self.FiltrateDialogs.mainView.BottomV.doneBtn addTag]subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            [self.FiltrateDialogs hide];
            [self.tbv.mj_header beginRefreshing];
        }];
    }
    return _FiltrateDialogs;
}



@end
